<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>Hiding Implementations</title>

    <style type="text/css">
        <!--
        .style1 {
            font-family: "Courier New", Courier, monospace
        }

        -->
    </style>
</head>
<body>
<h2><a name="hasi" id="hasi"></a>Hiding
    and Swapping of Implementations</h2>

<h3>Implementation Hiding
    <span class="subheading">(Located in PicoContainer Gems)</span></h3>

<p>This is where the implementation of the component is hidden
    from other components using it. The instance cannot be cast back to the
    implementation. It only works if the type has an interface that it
    implements. </p>

<div class="source">
<pre>pico = new DefaultPicoContainer(new ImplementationHiding());
pico.addComponent(Apple.class, AppleImpl.class);
Apple a1 = pico.getComponent(Apple.class);
// cannot cast back to AppleImpl
</pre>
</div>
<div class="source">
<pre>pico = new DefaultPicoContainer();
pico.as(HIDE_IMPL).addComponent(Apple.class, AppleImpl.class);
Apple a1 = pico.getComponent(Apple.class);
// cannot cast back to AppleImpl
</pre>
</div>
<div class="source">
<pre>pico = new PicoBuilder().withImplementationHiding().build();
pico.addComponent(Apple.class, AppleImpl.class);
Apple a1 = pico.getComponent(Apple.class);
// cannot cast back to AppleImpl
</pre>
</div>
<div class="source">
<pre>import static org.picocontainer.behaviors.Behaviors.implementationHiding;
...
pico = new PicoBuilder().withBehaviors(implementationHiding()).build();
pico.addComponent(Apple.class, AppleImpl.class);
Apple a1 = pico.getComponent(Apple.class);
// cannot cast back to AppleImpl
</pre>
</div>
<p>This behavior leverages Reflection's dynamic proxy capability.
</p>

<p>There's another implementation hiding behavior in Pico Gems
    called <span class="style1">AsmImplemenationHiding</span>
    that leverages ASM to make 'more concrete' hidden implementations than
    is possible via reflection. It generates real classes using <a href="http://asm.objectweb.org/">ASM</a></p>

<h3>Hot Swapping <span class="subheading">(Located in PicoContainer Gems)</span></h3>

<p>This builds on the <span class="style1">ASMImplementationHiding</span>
    behavior above, but also allows the hot swapping of component
    implementations during use. It has implicit caching behavior too.</p>

<div class="source">
<pre>DefaultPicoContainer pico = new DefaultPicoContainer(new HotSwapping());
pico.addComponent(Map.class, HashMap.class);
Map firstMap = pico.getComponent(Map.class);
firstMap.put(&quot;foo&quot;, &quot;bar&quot;);
HotSwappable hs = (HotSwappable) pico.getComponentAdapter(Map.class);
Object oldMap = hs.getSwappable().swap(new HashMap());
Map secondMap = pico.getComponent(Map.class);
secondMap.put(&quot;apple&quot;, &quot;orange&quot;);

// first map and second map are the same
// 'foo' is not a key in the map, wereas 'apple' is</pre>
</div>
</body>
</html>